package com.hnhd.eorder.db;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.hnhd.eorder.vo.Dish;
import com.hnhd.eorder.vo.Type;

/**
 * 菜品数据操作类
 * 
 * @author peng
 *
 */
public class DishUtil {

	public static Dish getDishById(String id) {
		Cursor cursor = SQLUtil.query(SQLUtil.DISH, null, "id=?", new String[] { id }, null,
				null, null);
		Dish dish = null;
		if (cursor.moveToFirst()) {
			do {
				dish = new Dish();
				dish.setId(cursor.getInt(0));
				dish.setName(cursor.getString(1));
				dish.setBrief(cursor.getString(2));
				dish.setLargePic(cursor.getString(3));
				dish.setMediumPic(cursor.getString(4));
				dish.setSmallPic(cursor.getString(5));
				dish.setModel(cursor.getString(6));
				dish.setNorm(cursor.getString(7));
				dish.setPrice(cursor.getFloat(8));
				dish.setGuqing(cursor.getInt(9));
				dish.setRemain(cursor.getInt(10));
				dish.setKey(cursor.getString(11));
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return dish;
	}

	/**
	 * 通过菜品类型获取对应的菜品
	 * 
	 * @param typeName
	 * @return dishes(菜品类型对应的菜品)
	 */
	public static ArrayList<Dish> getDishesByType(String typeName) {

		ArrayList<Dish> dishes = new ArrayList<Dish>();

		/** 获取菜品类型对应的菜品编号 */
		Type searchType = TypeUtil.getTypeByName(typeName);
		ArrayList<Integer> ids = new ArrayList<Integer>();
		ids = DishTypeUtil.getDishIdsByTypeId(searchType.getTypeId());

		for (int id : ids) {
			Dish dish = new Dish();
			dish = getDishById(String.valueOf(id));
			dishes.add(dish);
		}
		return dishes;
	}

	/**
	 * 通过菜品关键字(key)搜索对应的菜品
	 * 
	 * @param keyWord
	 * @return dishes(菜品类型对应的菜品)
	 */
	public static ArrayList<Dish> getDishesBySearch(String keyWord) {
		Cursor cursor = SQLUtil.getReadableDatabase()
				.rawQuery("SELECT * FROM DISH WHERE KEY LIKE %'" + keyWord
						+ "'%", null);
		ArrayList<Dish> dishes = new ArrayList<Dish>();
		if (cursor.moveToFirst()) {
			do {
				Dish dish = new Dish();
				dish.setId(cursor.getInt(0));
				dish.setName(cursor.getString(1));
				dish.setBrief(cursor.getString(2));
				dish.setLargePic(cursor.getString(3));
				dish.setMediumPic(cursor.getString(4));
				dish.setSmallPic(cursor.getString(5));
				dish.setModel(cursor.getString(6));
				dish.setNorm(cursor.getString(7));
				dish.setPrice(cursor.getFloat(8));
				dish.setGuqing(cursor.getInt(9));
				dish.setRemain(cursor.getInt(10));
				dish.setKey(cursor.getString(11));
				dishes.add(dish);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return dishes;
	}
	
	


	public static ArrayList<Dish> getAllDishes() {
		Cursor cursor = SQLUtil.getReadableDatabase().rawQuery("SELECT * FROM DISH",
				null);
		ArrayList<Dish> dishes = new ArrayList<Dish>();
		if (cursor.moveToFirst()) {
			do {
				Dish dish = new Dish();
				dish.setId(cursor.getInt(0));
				dish.setName(cursor.getString(1));
				dish.setBrief(cursor.getString(2));
				dish.setLargePic(cursor.getString(3));
				dish.setMediumPic(cursor.getString(4));
				dish.setSmallPic(cursor.getString(5));
				dish.setModel(cursor.getString(6));
				dish.setNorm(cursor.getString(7));
				dish.setPrice(cursor.getFloat(8));
				dish.setGuqing(cursor.getInt(9));
				dish.setRemain(cursor.getInt(10));
				dish.setKey(cursor.getString(11));
				dishes.add(dish);
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
			cursor.close();
		}
		return dishes;
	}

	public static long insertDish(Dish dish) {
		ContentValues values = new ContentValues();
		values.put("id", dish.getId());
		values.put("name", dish.getName());
		values.put("brief", dish.getBrief());
		values.put("largePic", dish.getLargePic());
		values.put("mediumPic", dish.getMediumPic());
		values.put("smallPic", dish.getSmallPic());
		values.put("model", dish.getModel());
		values.put("norm", dish.getNorm());
		values.put("price", dish.getPrice());
		values.put("isGuqing", dish.isGuqing());
		values.put("remain", dish.getRemain());
		values.put("key", dish.getKey());
		return SQLUtil.getWritableDatabase().insert(SQLUtil.DISH, null, values);
	}

	public static long updateDish(Dish dish) {
		ContentValues values = new ContentValues();
		values.put("id", dish.getId());
		values.put("name", dish.getName());
		values.put("brief", dish.getBrief());
		values.put("largePic", dish.getLargePic());
		values.put("mediumPic", dish.getMediumPic());
		values.put("smallPic", dish.getSmallPic());
		values.put("model", dish.getModel());
		values.put("norm", dish.getNorm());
		values.put("price", dish.getPrice());
		values.put("isGuqing", dish.isGuqing());
		values.put("remain", dish.getRemain());
		values.put("key", dish.getKey());
		return SQLUtil.getWritableDatabase().update(SQLUtil.DISH,values,"id=?",new String[]{String.valueOf(dish.getId())});
	}

	public static void deleteDish(Dish dish) {
		SQLUtil.getWritableDatabase().delete(SQLUtil.DISH , "id=?" , new String[]{String.valueOf(dish.getId())});
	}
}
